package com.platform.qujia.utils;

import org.apache.ibatis.executor.result.ResultMapException;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeException;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;

/**
 * appUserInfo表mobilePhone字段typeHandler
 * 当手机号是系统自动生成时，返回null，标识用户未领取会员卡，是一个游客身份
 * @date 2023/8/29
 */
public class MobilePhoneTypeHandler implements TypeHandler<String> {
    public MobilePhoneTypeHandler() {
    }

    public void setParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
        if (parameter == null) {
            if (jdbcType == null) {
                throw new TypeException("JDBC requires that the JdbcType must be specified for all nullable parameters.");
            }

            try {
                ps.setNull(i, jdbcType.TYPE_CODE);
            } catch (SQLException var7) {
                throw new TypeException("Error setting null for parameter #" + i + " with JdbcType " + jdbcType + " . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: " + var7, var7);
            }
        } else {
            try {
                ps.setString(i, parameter);
            } catch (Exception var6) {
                throw new TypeException("Error setting non null for parameter #" + i + " with JdbcType " + jdbcType + " . Try setting a different JdbcType for this parameter or a different configuration property. Cause: " + var6, var6);
            }
        }

    }

    public String getResult(ResultSet rs, String columnName) throws SQLException {
        try {
            return returnValue(rs.getString(columnName));
        } catch (Exception var4) {
            throw new ResultMapException("Error attempting to get column '" + columnName + "' from result set.  Cause: " + var4, var4);
        }
    }

    public String getResult(ResultSet rs, int columnIndex) throws SQLException {
        try {
            return returnValue(rs.getString(columnIndex));
        } catch (Exception var4) {
            throw new ResultMapException("Error attempting to get column #" + columnIndex + " from result set.  Cause: " + var4, var4);
        }
    }

    public String getResult(CallableStatement cs, int columnIndex) throws SQLException {
        try {
            return returnValue(cs.getString(columnIndex));
        } catch (Exception var4) {
            throw new ResultMapException("Error attempting to get column #" + columnIndex + " from callable statement.  Cause: " + var4, var4);
        }
    }

    private String returnValue(String val){
        if (MobilePhoneUtil.isFakePhone(val)) {
            return null;
        }
        return val;
    }

}
